define(
    'alarm',
    [
        //引入所依赖的文件
        "avalon",
        'text!../../package/Alarm/alarm.html',
        'css!../../package/Alarm/alarm.css'
    ],
    function (avalon,html,css) {
        var model = avalon.define({
            $id: "alarm",
            clcapm: true,    //闹钟上下午 true:am、flase:pm
            clchh: 0,    //闹钟时
            clcmm: 0,    //闹钟分
            clconoff: false,       //默认闹钟关闭
            nowy: 0,     //年
            nowm: 0,     //月
            nowd: 0,     //日
            nowhh: 0,    //时
            nowmm: 0,    //分
            nowss: 0,    //秒
            dayIndex: 0,     //星期几
            dayArr: ["日", "一", "二", "三", "四", "五", "六"],
            tmform: 12,      //时制 12 or 24
            onalltmh: 0,     //记录24小时制的 当前时
            ready: function () {
                index.content = html;
                model.init();
                model.updatatm();
            },
            init:function(){         //初始化闹钟
                var dataObj = new Date();
                model.nowy = dataObj.getFullYear();   //四位数的年份
                model.nowm = dataObj.getMonth();  //月份 0-11
                model.nowd = dataObj.getDate();       //一个月中的某一天 1-31
                model.nowhh = dataObj.getHours();     //小时 0-23
                model.onalltmh = model.nowhh;
                if (model.tmform == 12) {
                    if (model.nowhh > 12) {
                        model.nowhh -= 12;
                    }
                }
                model.nowmm = dataObj.getMinutes();      //分钟 0-59
                model.nowss = dataObj.getSeconds();       //秒 0-59
                model.dayIndex = dataObj.getDay();    //一周中的某一天 0-6
            },
            updatatm: function () {        //更新时间
                model.nowss++;
                if (model.nowss == 60) {
                    model.nowss = 0;
                    model.nowmm++;
                    if (model.nowmm == 60) {
                        model.nowmm = 0;
                        model.init();
                    }
                    model.ifOntm();     //判断是否到闹铃时间
                }
                setTimeout(model.updatatm, 1000);
            },
            ifOntm: function () {      //比较时间是否到达闹铃时间
                if (model.clconoff) {     //开启闹钟
                    if (model.clcapm) {
                        var tmpclch = model.clchh;
                    } else {
                        var tmpclch = model.clchh + 12;
                    }
                    if (model.onalltmh == tmpclch && model.nowmm == model.clcmm) {
                        model.startMusic();     //响起闹铃
                    }
                }
            },
            startMusic: function () {      //响起闹铃
                if (model.clconoff) {     //开启闹钟
                    var $clcmusic = document.getElementById("clcmusic");
                    $clcmusic.play();
                }
            }
        });
        model.$watch("tmform",function(){
            if(model.tmform == 12){
                if(model.nowhh > 12){
                    model.nowhh -= 12;
                }
            }else{
                var dataObj = new Date();
                model.nowhh = dataObj.getHours();  //小时 0-23
            }
        });
        model.$watch("clconoff",function(){
            if(model.clconoff == false){
                var $clcmusic = document.getElementById("clcmusic");
                $clcmusic.pause();
            }
        });
        //扫描
        avalon.scan();
});
